package com.example.zjx8969.myapplication.view

import android.content.Context
import android.graphics.*
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import com.example.zjx8969.myapplication.DensityUtil
import kotlin.math.min

class LinearGradientTextView @JvmOverloads constructor(
        context: Context,
        attributeSet: AttributeSet? = null,
        defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {
//        private var gradient: LinearGradient = LinearGradient(0f, 0f, 10f, 10f, Color.RED, Color.BLUE, Shader.TileMode.CLAMP)
//    private var gradient = RadialGradient(10f, 10f, 10f, Color.RED, Color.BLUE, Shader.TileMode.CLAMP)
    val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
//        color = Color.BLACK
//        shader = gradient
        textSize = DensityUtil.dp2px(16f)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        paint.shader = RadialGradient(measuredWidth.shr(1).toFloat(), measuredHeight.shr(1).toFloat(), min(measuredWidth.shr(1), measuredHeight.shr(1)).toFloat(), Color.RED, Color.BLUE, Shader.TileMode.CLAMP)
        paint.shader  = LinearGradient(measuredWidth.shr(1).toFloat(), measuredHeight.shr(1).toFloat(), measuredWidth.toFloat(), measuredHeight.toFloat(), Color.RED, Color.BLUE, Shader.TileMode.CLAMP)
    }

    override fun onDraw(canvas: Canvas?) {
        canvas?.drawArc(0f, 0f, measuredWidth.toFloat(), measuredHeight.toFloat(), 0f, 360f, true, paint)
//        canvas?.drawRect(0f, 0f, 100f, 100f, paint)
    }


}